/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.alertnetusa.validator;

import com.incident.cap.*;

/** A EasCapResource object within an Info element of a CAP 1.1 message.<p>
 * <i>Copyright (c) 2008-2009 Thai Van<p>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * @see EasCapInfo
 * @author Thai Van, Prafash Kumar. This class is based on Art Botterell, David R Robinson work on com.incident.cap.Alert
 * @version 1 Oct 2008
 */
public class EasCapResource {

    private StringBuffer buf;         
    /** EasCapResource resourceDesc */
    protected String resourceDesc = null;
    /** EasCapResource mimeType */
    protected String mimeType = null;     // MIME Type
    /** EasCapResource size */
    protected String size = null;         // Size
    /** EasCapResource uri */
    protected String uri = null;          // URI
    /** EasCapResource derefUri */
    protected String derefUri = null;     // derefUri
    /** EasCapResource digest */
    protected String digest = null;       // Digest

    /** Gets resource description */
    public String getResourceDesc() {
        return resourceDesc;
    }

    /** Sets resource description */
    public void setResourceDesc(String value) {
        resourceDesc = value;
    }

    /** Gets MIME type */
    public String getMimeType() {
        return mimeType;
    }

    /** Sets MIME type */
    public void setMimeType(String value) {
        mimeType = value;
    }

    /** Gets resource size in bytes */
    public String getSize() {
        return size;
    }

    /** Sets resource size in bytes */
    public void setSize(String value) {
        size = value;
    }

    /** Gets resource URI */
    public String getUri() {
        return uri;
    }

    /** Sets resource URI */
    public void setUri(String value) {
        uri = value;
    }

    /** Gets base64 encoded string */
    public String getDerefUri() {
        return derefUri;
    }

    /** Sets base-64 encoded string */
    public void setDerefUri(String value) {
        derefUri = value;
    }

    /** base-64-encode and set derefUri from an unencoded string */
    public void encodeDerefUri(String value) {
        derefUri = Base64.encodeBytes(value.getBytes());
    }

    /** get and base-64-decode derefUri to an unencoded string */
    public String decodeDerefUri() {
        return new String(Base64.decode(derefUri));
    }

    /** load and base-64-encode from file */
    public void loadDerefUri(String filename) {
        derefUri = Base64.encodeFromFile(filename);
    }

    /** base-64-decode and store to file */
    public void storeDerefUri(String filename) {
        Base64.decodeToFile(derefUri, filename);
    }

    /** Gets resource digest */
    public String getDigest() {
        return digest;
    }

    /** Sets resource digest */
    public void setDigest(String value) {
        digest = value;
    }

    /** Return raw EAS-CAP Resource fragment */
    public String toEASCAP() {
        buf = new StringBuffer();

        if (resourceDesc != null) {
            buf.append("<resourceDesc>" + EasCapAlert.entities(resourceDesc) + "</resourceDesc>\n");
        }

        if (mimeType != null) {
            buf.append("<mimeType>" + EasCapAlert.entities(mimeType) + "</mimeType>\n");
        }

        if (size != null) {
            buf.append("<size>" + EasCapAlert.entities(size) + "</size>\n");
        }

        if (uri != null) {
            buf.append("<uri>" + EasCapAlert.entities(uri) + "</uri>\n");
        }

        if (derefUri != null) {
            buf.append("<derefUri>" + EasCapAlert.entities(derefUri) + "</derefUri>\n");
        }

        if (digest != null) {
            buf.append("<digest>" + EasCapAlert.entities(digest) + "</digest>\n");
        }

        // return StringBuffer content
        return buf.toString();
    }

    /** Overridden toString() to return an XML fragment */
    public String toString() {
        // build resource contents in StringBuffer
        buf = new StringBuffer();
        if (resourceDesc != null) {
            buf.append("<resourceDesc>" + EasCapAlert.entities(resourceDesc) + "</resourceDesc>\n");
        } else {
            buf.append("<resourceDesc>" + EasCapAlert.entities("Undefined resource") + "</resourceDesc>\n");
        }
        if (mimeType != null) {
            buf.append("<mimeType>" + EasCapAlert.entities(mimeType) + "</mimeType>\n");
        }
        if (size != null) {
            buf.append("<size>" + EasCapAlert.entities(size) + "</size>\n");
        }
        if (uri != null) {
            buf.append("<uri>" + EasCapAlert.entities(uri) + "</uri>\n");
        }
        if (derefUri != null) {
            buf.append("<derefUri>" + EasCapAlert.entities(derefUri) + "</derefUri>\n");
        }
        if (digest != null) {
            buf.append("<digest>" + EasCapAlert.entities(digest) + "</digest>\n");
        }
        // return StringBuffer content
        return buf.toString();
    }

    /** return CAP 1.0 XML fragment */
    public String toCAP10() {
        // build resource contents in StringBuffer
        buf = new StringBuffer();
        if (resourceDesc != null) {
            buf.append("<resourceDesc>" + EasCapAlert.entities(resourceDesc) + "</resourceDesc>\n");
        } else {
            buf.append("<resourceDesc>" + EasCapAlert.entities("Undefined resource") + "</resourceDesc>\n");
        }
        if (mimeType != null) {
            buf.append("<mimeType>" + EasCapAlert.entities(mimeType) + "</mimeType>\n");
        }
        if (size != null) {
            buf.append("<size>" + EasCapAlert.entities(size) + "</size>\n");
        }
        if (uri != null) {
            buf.append("<uri>" + EasCapAlert.entities(uri) + "</uri>\n");
        }
        if (derefUri != null) {
            buf.append("<derefUri>" + EasCapAlert.entities(derefUri) + "</derefUri>\n");
        }
        if (digest != null) {
            buf.append("<digest>" + EasCapAlert.entities(digest) + "</digest>\n");
        }
        // return StringBuffer content
        return buf.toString();
    }
}
